package com.ljb.commlibrary.utils;

import android.text.TextUtils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 字符串工具类
 * Created by ljb on 2018/4/28.
 */

public class StringUtils {
    private StringUtils() {
    }

    /**
     * 判断多个字符串是否为空
     * @param strs
     * @return
     */
    public static boolean isEmpty(CharSequence... strs) {
        for (CharSequence str : strs) {
            if (!TextUtils.isEmpty(str))
                return false;
        }
        return true;
    }

    /**
     * String类型的字符串进行MD5编码
     *
     * @param data 需要进行编码的原始数据
     * @return 编码后的数据
     */
    public static String StringToMD5(String data) {
        String result;
        try {
            MessageDigest mDigest = MessageDigest.getInstance("MD5");
            mDigest.update(data.getBytes());
            result = bytesToHexString(mDigest.digest());
        } catch (NoSuchAlgorithmException e) {
            result = String.valueOf(data.hashCode());
        }
        return result;
    }

    /************************一条神奇的分割线*********************************/

    private static String bytesToHexString(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < bytes.length; i++) {
            String hex = Integer.toHexString(0xFF & bytes[i]);
            if (hex.length() == 1) {
                sb.append('0');
            }
            sb.append(hex);
        }
        return sb.toString();
    }
}
